

/*------------------------------------------------------------------------------
	1. Housekeeping
------------------------------------------------------------------------------*/

	*Run housekeeping code
		qui do ".../replication_package/housekeeping.do"

	*Write log
		log using "$log_loc/figure_a8.log", text replace

/*------------------------------------------------------------------------------
	2. Clean Main dataset
------------------------------------------------------------------------------*/

		*Import Data
		use "$data/childrens2018_w_w2.dta", clear
			drop _merge

			fmerge m:1 in_data using "$data/filed_flag_2018.dta"
				drop if _merge == 2
				ren (_merge) (in_data_flag)

			/*Restrict Sample*/
				*Keep 1095 filing
					rename state_insurance state
					fmerge m:1 state using "$data/statelist.dta"
						assert _merge != 2 //Confirm that all 51 states are in dataset
						drop if _merge != 3
						drop _merge

				*Keep if age_num is within the range
					keep if age_num >= 0 & age_num <= 16

				*Keep if kid died after end of year or is currently alive
					keep if 2018*10000+1231<death_date|death_date==0

				*Keep if months covered is possible
					keep if num_cov_ins > 0 & num_cov_ins <= 12
	
			/*Generate variables for figure*/
				*Generate a ct variable
					gen ct = 1

			*Save datasets as a tempfile
			tempfile inc_2018
			save `inc_2018', replace

	*Loop through years:
		*Import Data
		use "$data/childrens2021_w_w2.dta", clear
			drop _merge

			fmerge m:1 in_data using "$data/filed_flag_2021.dta"
				drop if _merge == 2
				ren (_merge) (in_data_flag)

			/*Restrict Sample*/
				*Keep 1095 filings
					rename state_insurance state
					fmerge m:1 state using "$data/statelist.dta"
						assert _merge != 2 //Confirm that all 51 states are in dataset
						drop if _merge != 3
						drop _merge

				*Keep if age_num is within the range
					keep if age_num >= 0 & age_num <= 16

				*Keep if kid died after end of year or is currently alive
					keep if 2021*10000+1231<death_date|death_date==0

				*Keep if months covered is possible
					keep if num_cov_ins > 0 & num_cov_ins <= 12
	
			/*Generate variables for figure*/

				*Generate a ct variable
					gen ct = 1

			*Append datasets
				append using `inc_2018'

				gen onreturn = file_inc != . & flag_filed_in_yr1 == 1

				*Rescale Claimed Variable to be between 1 and 100
					replace onreturn = onreturn * 100

				*Adjust for claiming limits
					replace onreturn = onreturn * 1.0017029

				*Create outcome for joint income
					gen tot_med_wage_nums = dad_med_wage_nums + mom_med_wage_nums				

				compress
				
/*------------------------------------------------------------------------------
	4. Create Figures
------------------------------------------------------------------------------*/

			/*Generate figures*/
					binscatter onreturn mom_med_wage_nums if mom_med_wage_nums > 0 & mom_med_wage_nums < 100000 & year == 2018, nq(100) savedata("$data/mom_2018.csv") replace

					binscatter onreturn mom_med_wage_nums if mom_med_wage_nums > 0 & mom_med_wage_nums < 100000 & year == 2021, nq(100) savedata("$data/mom_2021.csv") replace

					binscatter onreturn tot_med_wage_nums if tot_med_wage_nums > 0 & tot_med_wage_nums < 100000 & year == 2018, nq(100) savedata("$data/joint_2018.csv") replace

					binscatter onreturn tot_med_wage_nums if tot_med_wage_nums > 0 & tot_med_wage_nums < 100000 & year == 2021, nq(100) savedata("$data/joint_2021.csv") replace


			insheet using "$data/mom_2018.csv.csv", clear
				gen year = 2018
				tempfile mom_2018
				save `mom_2018', replace

			insheet using "$data/mom_2021.csv.csv", clear
				gen year = 2021
				append using `mom_2018'

			twoway (scatter onreturn mom_med_wage_nums if year == 2021, mcolor(black) msymbol(Oh)) ///
				(scatter onreturn mom_med_wage_nums if year == 2018, mcolor(black) msymbol(d)), ///
				 graphregion(fcolor(white)) xtitle("Wages ($)") ytitle("% Children Claimed") ///
				xlabel(0(20000)100000, format(%10.0fc) labsize(small)) ///
				legend(label(1 "2021") label(2 "2018") rows(2) order (2 1) size(small) pos(0) bplace(seast) region(lcolor(black)  lwidth(thin))) 

				graph export "${output}/mom income 2018 vs 2021.png", replace as(png)
				

			insheet using "$data/joint_2018.csv.csv", clear
				gen year = 2018
				tempfile joint_2018
				save `joint_2018', replace

			insheet using "$data/joint_2021.csv.csv", clear
				gen year = 2021
				append using `joint_2018'


			twoway (scatter onreturn tot_med_wage_nums if year == 2021, mcolor(black) msymbol(Oh)) ///
				(scatter onreturn tot_med_wage_nums if year == 2018, mcolor(black) msymbol(d)), ///
				 graphregion(fcolor(white)) xtitle("Wages ($)") ytitle("% Children Claimed") ///
				xlabel(0(20000)100000, format(%10.0fc) labsize(small)) ///
				legend(label(1 "2021") label(2 "2018") rows(2) order (2 1) size(small) pos(0) bplace(seast) region(lcolor(black) lwidth(thin))) 

				graph export "${output}/joint income 2018 vs 2021.png", replace as(png)